#include "cmdline/Parse.h"
#include "line.h"
#include "line_info.h"
#include "output.h"

using namespace std;
using namespace tinytool;

static void line_cmdline_parse(int argc, char* argv[])
{
    cmdline::Parse parse;
    parse.add_more<std::string>("input", 'i', "the input paths", true, true);
    parse.set_program_name(LINE_PROGRAM_NAME);

    if (!parse.parse(argc, argv)) {
        if (parse.exist('s')) {
            std::cout << parse.show() << std::endl;
        }
        else if (parse.exist('h')) {
            std::cout << parse.usage() << std::endl;
        }
        else if (parse.exist('v')) {
            cout_log(parse.program_name(), LINE_VERSION);
        }
        else {
            cout_error(parse.program_name(), "cmdline parse errors");
            std::cout << parse.errors() << std::endl;
        }
    }
    else {
        if (parse.exist('s')) {
            std::cout << parse.show() << std::endl;
        }
        else if (parse.exist('h')) {
            std::cout << parse.usage() << std::endl;
        }
        else if (parse.exist('v')) {
            cout_log(parse.program_name(), LINE_VERSION);
        }
        else {
            auto   path = parse.get_values('i');
            line_t line = 0;
            for (auto it : path) {
                line += line::line(it);
            }
            cout_log(LINE_PROGRAM_NAME, "total lines: " + mark(line));
        }
    }
}

int main(int argc, char* argv[])
try {
    line_cmdline_parse(argc, argv);
    return 0;
}
catch (const std::exception& e) {
    cout_error(LINE_PROGRAM_NAME, e.what());
    return 1;
}